// audit.js
const app = getApp()
let that
let userInfo
Page({
  data: {
    barragerItems: {},
    isLoading: 1,
    autoLoad: 0
  },

  onLoad: function (options) {
    that = this
    userInfo = wx.getStorageSync('userInfo') || false
    if (userInfo) app.reg(userInfo)
    that.reload()
  },

  reload: function () {
    that.setData({
      isLoading: 1
    })
    app.ajax({
      url: "/wechat/Barrager/admin",
      success: function (json) {
        that.setData({
          barragerItems: json.items ? json.items : {},
          isLoading: 0
        })
        if (JSON.stringify(that.data.barragerItems) === "{}" && that.data.autoLoad === 1) {
          setTimeout(function () {
            that.reload()
          }, 3000)
        }
      }
    })
  },

  onShow: function () {
    that.setData({
      autoLoad: 1
    })
  },

  onUnload: function () {
    that.setData({
      autoLoad: 0
    })
  },

  operation: function (e) {
    let idx = e.currentTarget.dataset.idx
    let cmd = e.currentTarget.dataset.cmd
    let id = e.currentTarget.dataset.id
    switch (cmd) {
      case "push":
        app.ajax({
          url: "/wechat/Barrager/update",
          data: { id: id, state: 2 },
          success: function () {
            that.data.barragerItems.splice(idx, 1)
            that.setData({
              barragerItems: that.data.barragerItems
            });
          }
        });
        break
      case "del":
        app.ajax({
          url: "/wechat/Barrager/update",
          data: { id: id, state: 4 },
          success: function () {
            that.data.barragerItems.splice(idx, 1)
            that.setData({
              barragerItems: that.data.barragerItems
            });
          }
        });
        break
    }
  }
})